package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.activitys.AppContext;
import com.astrob.model.LonLat;
import com.astrob.model.Msg;
import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.util.Utils;
import com.astrob.view.BaseViewHolder;
import com.gpx2shp.JniUtils;
import com.nhc.database.dbmodel.Measure;
import com.nhc.database.dbmodel.MeasurePoint;
import com.nhc.database.dbmodel.Route;
import com.nhc.database.dbmodel.TrackPoint;
import com.nhc.database.dbmodel.WayPoint;
import com.nhc.database.dbmodel.WayTrail;
import com.nhc.database.manager.DBDataCenter;
import com.nhc.database.manager.IECenter;
import com.nhc.database.manager.MeasureCenter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.AlertDialog.OnDialogButtonClickListener;
import zuo.biao.library.ui.EditTextInfoActivity;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.ui.ItemDialog;
import zuo.biao.library.ui.PageScroller;
import zuo.biao.library.ui.TopMenuWindow;
import zuo.biao.library.util.StringUtil;

/**
 * 使用方法：复制>粘贴>改名>改代码
 */

/**
 * activity示例
 *
 * @warn 复制到其它工程内使用时务必修改import R文件路径为所在应用包名
 * @use toActivity(DemoActivity.createIntent(...));
 */
public class MeasureActivity extends BaseActivity implements OnDialogButtonClickListener, ItemDialog.OnDialogItemClickListener {
    private final String TAG = "MeasureActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, MeasureActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.measure_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lvMeasure;
    private Button bt_do;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE
        lvMeasure = (ListView) findViewById(R.id.lvMeasure);
        bt_do = (Button) findViewById(R.id.bt_do);

    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<Integer, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lvMeasure.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<Integer, String>> measurelist;
    private ArrayList<Measure> mMeasureList = new ArrayList<Measure>();

    private int mEditMode = 0;//0-一般  1-选择删除 2--选择导出
    private HashMap<Integer, Boolean> hashMap;

    private boolean getItemChecked(int position) {
        return hashMap.get(position);
    }

    private void setItemChecked(int position, boolean isChecked) {
        if (adapter != null) {
            hashMap.put(position, isChecked);
            adapter.notifyDataSetChanged();
        }
    }

    //示例代码>>>>>>>>>
    private void resetMeasure() {
        MeasureCenter.getInstance().doQueryMeasure();
        mMeasureList = MeasureCenter.getInstance().getMeasureList();
    }

    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                resetMeasure();
                measurelist = getList();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(measurelist);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    protected List<Entry<Integer, String>> getList() {
        measurelist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < mMeasureList.size(); i++) {
            measurelist.add(new Entry<Integer, String>(i, ""));
        }
        return measurelist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lvMeasure.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (mEditMode > 0 && null != hashMap) {
                    boolean isSelected = hashMap.get(position);
                    hashMap.put(position, !isSelected);
                    if (adapter != null) {
                        adapter.notifyDataSetChanged();
                    }
                    boolean haveselected = false;
                    for (int i = 0; i < mMeasureList.size(); i++) {
                        if (hashMap.get(i)) {
                            haveselected = true;
                            break;
                        }
                    }
                    if (haveselected) {
                        if (mEditMode == 2) {
                            bt_do.setText(getString(R.string.daochu));
                        } else {
                            bt_do.setText(getString(R.string.delete));
                        }
                    } else {
                        bt_do.setText(getString(R.string.cancel));
                    }
                } else {
                    selecteMeasure(position);
                }
            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lvMeasure).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    public static final String RESULT_VALUE = "RESULT_VALUE";

    private void selecteMeasure(int index) {
        if (index < 0 || index >= mMeasureList.size()) {
            return;
        }
        mIndex = index;
        new ItemDialog(context, getResources().getStringArray(R.array.top_measure_pop), "", DIALOG_SET_TOPBAR2, this).show();

    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法

    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应

        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu_mesure)), REQUEST_TO_TOP_MENU, false);
    }

    public void onSelectAll(View v) {
        selectAll(true);
    }

    public void onSelectAllNO(View v) {
        selectAll(false);
    }

    public void selectAll(boolean isSelected) {
        if (hashMap == null) {
            hashMap = new HashMap<Integer, Boolean>();
        }
        hashMap.clear();
        if (measurelist != null) {
            for (int i = 0; i < measurelist.size(); i++) {
                hashMap.put(i, isSelected);
            }
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }

        if (isSelected) {
            if (mEditMode == 2) {
                bt_do.setText(getString(R.string.daochu));
            } else {
                bt_do.setText(getString(R.string.delete));
            }
        } else {
            bt_do.setText(getString(R.string.cancel));
        }
    }

    private final String[] TOPBAR_NAMES = {"GPX", "KML", "SHP"};

    public void onDelSelected(View v) {

        boolean haveselected = false;
        for (int i = 0; i < mMeasureList.size(); i++) {
            if (hashMap.get(i)) {
                haveselected = true;
                break;
            }
        }

        if (haveselected) {
            if (mEditMode == 2) {
                new ItemDialog(context, TOPBAR_NAMES, getString(R.string.daochu_item), DIALOG_SET_TOPBAR1, this).show();
            } else {
                new AlertDialog(context, getString(R.string.wxts), getString(R.string.qscxzdcljl), true, 0, this).show();
            }
        } else {
            mEditMode = 0;
            findViewById(R.id.ctrl_measure_edit).setVisibility(View.GONE);
            findViewById(R.id.ctrl_measure_add).setVisibility(View.VISIBLE);

            if (adapter != null) {
                adapter.notifyDataSetChanged();
            }
        }

    }

    private void delSelected() {
        mEditMode = 0;
        findViewById(R.id.ctrl_measure_edit).setVisibility(View.GONE);
        findViewById(R.id.ctrl_measure_add).setVisibility(View.VISIBLE);
        showShortToast(getString(R.string.deleteok));

        ArrayList<Long> pkIds = new ArrayList<Long>();

        for (int i = 0; i < mMeasureList.size(); i++) {
            if (hashMap.get(i)) {
                pkIds.add(mMeasureList.get(i).getId());
            }
        }
        MeasureCenter.getInstance().doDeleteMeasure(pkIds);
        initData();

    }

    private void clearListData() {
        MeasureCenter.getInstance().doDeleteAllMeasure();
        initData();
    }

    private void onToEdit() {
        if (measurelist.size() < 1) {
            showShortToast(getString(R.string.tnotrackdelete));
            return;
        }

        mEditMode = 1;
        findViewById(R.id.ctrl_measure_edit).setVisibility(View.VISIBLE);
        findViewById(R.id.ctrl_measure_add).setVisibility(View.GONE);

        selectAll(false);
    }

    private void onToExport() {
        if (measurelist.size() < 1) {
            showShortToast(getString(R.string.tnotrackexport));
            return;
        }

        mEditMode = 2;
        findViewById(R.id.ctrl_measure_edit).setVisibility(View.VISIBLE);
        findViewById(R.id.ctrl_measure_add).setVisibility(View.GONE);

        selectAll(false);
    }

    public void onAddMeasure(View v) {
//        MeasureCenter.getInstance().addNewMeasure();
//        initData();
        new ItemDialog(context, getResources().getStringArray(R.array.top_bar_mesure), "", DIALOG_SET_TOPBAR, this).show();
    }

    //pos   0-实时测距   1-面积测量 2-实时面积测量  3-等宽测量  4 坡面测量
    private void onDoNext(int pos) {
        switch (pos) {
            default:
            case 1:
                toActivity(MeasureMapActivity.createIntent(context, pos), REQUEST_MEASURE);
                break;
            case 0:
            case 2:
            case 3:
            case 4:
                checkMeasure(pos);
                break;
        }

    }

    private void doRealtimeareaSave() {
        Measure measureRT = MeasureCenter.getInstance().measureRT;
        if (measureRT == null || measureRT.measurePtList == null
                || measureRT.measurePtList.size() < 2) {
            showShortToast(getString(R.string.celiang_toast_0));
            return;
        }

        intent = EditTextInfoActivity.createIntent(context, EditTextInfoActivity.TYPE_NICK
                , getString(R.string.sscl_name), SysUtils.getInstance().getTimeStr(System.currentTimeMillis()));
        toActivity(intent, REQUEST_TO_EDIT_TEXT_INFO2, false);

    }

    private void measureSaveRt(String name) {
        Measure measureRT = MeasureCenter.getInstance().measureRT;
        if (measureRT == null || measureRT.measurePtList == null
                || measureRT.measurePtList.size() < 2) {
            showShortToast(getString(R.string.celiang_toast_0));
            return;
        }
        if (name != null && name.length() > 1) {
            measureRT.setName(name);
        }
        measureRT.setType(1);
        final Long measureId = MeasureCenter.getInstance().saveMeasure(measureRT);

        showProgressDialog(R.string.saving);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                Measure measure = MeasureCenter.getInstance().getMeasureById(measureId);
                ArrayList<LonLat> measureList = new ArrayList<LonLat>();
                int size = measure.measurePtList.size();
                for (int i = 0; i < size; i++) {
                    MeasurePoint mp = measure.measurePtList.get(i);
                    mp.setMeasure_id(measureId);
                    MeasureCenter.getInstance().saveMeasurePoint(mp);
                    measureList.add(mp.getLL());
                }
                measure.num = size;
                measure.setPerimeter(Utils.calculatePerimeter(measureList));
                measure.setArea(Utils.calculateArea(measureList));
                measure.setLength(Utils.calculateLen(measureList));
                MeasureCenter.getInstance().saveMeasure(measure);

                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
//                        showShortToast(getString(R.string.ssclsjybc));
//                        toActivity(MeasureInfoActivity.createIntent(context, measureId));
                        MeasureCenter.getInstance().measureRT = null;

                        //
                        doMeasure();
                    }
                });
            }
        });

    }

    int _type = 0;

    private void checkMeasure(int pos) {
        _type = pos;

        Measure measureRT = MeasureCenter.getInstance().measureRT;
        if (measureRT == null || measureRT.measurePtList == null
                || measureRT.measurePtList.size() < 2) {
            doMeasure();
        } else {
            new AlertDialog(context, "", getString(R.string.sfbczzjxdsscl), true, 11, this).show();
        }

    }

    private void doMeasure() {
        switch (_type) {
            default:
                break;
            case 0:
                MeasureCenter.getInstance().measureRT = new Measure("realtimeLen");
                showShortToast(getString(R.string.yjkssscj));
                beginMesure();
                break;
            case 2:
                MeasureCenter.getInstance().measureRT = new Measure("realtimeArea");
                showShortToast(getString(R.string.yjkqlssmjcl));
                beginMesure();
                break;
            case 3:
                onSetWidth();
                break;
            case 4:
                onSetSlopeangle();
                break;
        }
    }

    void beginMesure() {

        //test
//        LonLat ll = SysUtils.getInstance().getLL();
//
//        MeasureCenter.getInstance().measureRT.add(ll, 50);
//        MeasureCenter.getInstance().measureRT.add(new LonLat(ll.GetLon() + 0.0005, ll.GetLat() + 0.0005), 55);
//        MeasureCenter.getInstance().measureRT.add(new LonLat(ll.GetLon() + 0.005, ll.GetLat() - 0.005), 60);

        setResult(Msg.NAVI_BY_ROUTE);
        finish();
    }

    private void onSetWidth() {
        String AA[] = SysUtils.getInstance().getAltitudeStr(SysUtils.getInstance().measure_width);
        intent = EditNumberActivity.createIntentD(context
                , getString(R.string.szkd), Double.valueOf(AA[0]), AA[1]);
        toActivity(intent, REQUEST_TO_WIDTH);
    }

    private void onSetSlopeangle() {
        intent = EditNumberActivity.createIntentJ(context
                , getString(R.string.pdj), (int)SysUtils.getInstance().measure_slopeangle, getString(R.string.du));
        toActivity(intent, REQUEST_TO_SLOPA);
    }

    private final int REQUEST_TO_TOP_MENU = 30;
    private final int REQUEST_TO_MEASUREDETAIL = 31;
    private final int REQUEST_MEASURE = 32;
    private final int REQUEST_TO_WIDTH = 45;
    private final int REQUEST_TO_SLOPA = 46;
    private final int REQUEST_TO_EDIT_TEXT_INFO2 = 47;
    private final int REQUEST_TO_EDIT_TEXT_INFO3 = 48;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case REQUEST_TO_EDIT_TEXT_INFO3:
                if (data != null) {
                    String name = StringUtil.getTrimedString(
                            data.getStringExtra(EditTextInfoWindow.RESULT_VALUE));
                    if (_measureEdit != null) {
                        _measureEdit.setName(name);
                        MeasureCenter.getInstance().saveMeasure(_measureEdit);
                        initData();
                    }
                }
                break;
            case REQUEST_TO_EDIT_TEXT_INFO2:
                if (data != null) {
                    String name = StringUtil.getTrimedString(
                            data.getStringExtra(EditTextInfoWindow.RESULT_VALUE));
                    measureSaveRt(name);
                }
                break;
            case REQUEST_MEASURE:
                initData();
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            onToEdit();
                            break;
                        case 1:
                            if (measurelist.size() > 0) {
                                new AlertDialog(context, getString(R.string.wxts), getString(R.string.qdqkcl), true, 1, this).show();
                            } else {
                                showShortToast(getString(R.string.swjl));
                            }
                            break;
                        case 2:
                            onToExport();
                            break;
                        default:
                            break;
                    }
                }
                break;
            case REQUEST_TO_WIDTH:
                if (data != null) {
                    String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);

                    double distance = Double.valueOf(editedValue);
                    switch (SysUtils.getInstance().dis_mode) {
                        default:
                            break;
                        case 1:
                        case 4:
                            //ft => m
                            distance = distance / 3.2808399;
                            break;
                        case 2: {
                            //yd => m
                            distance = distance / 1.0936133;
                        }
                        break;
                    }

                    MeasureCenter.getInstance().measureRT = new Measure("realtimeArea_samewidth");
                    MeasureCenter.getInstance().measureRT.setAreatype(1);
                    MeasureCenter.getInstance().measureRT.setType(1);
                    MeasureCenter.getInstance().measureRT.setWidth(distance);
                    SysUtils.getInstance().measure_width = distance;
                    SysUtils.getInstance().saveMeasureParam();
                    showShortToast(getString(R.string.ykqlssdkcl));
                    beginMesure();
                }
                break;
            case REQUEST_TO_SLOPA:
                if (data != null) {
                    String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);
                    int slopeangle = Integer.valueOf(editedValue);
                    if (slopeangle < 0 || slopeangle >= 90) {
                        showShortToast(getString(R.string.pdjydylxyjsd));
                        return;
                    }
                    MeasureCenter.getInstance().measureRT = new Measure("realtimeArea_slope");
                    MeasureCenter.getInstance().measureRT.setAreatype(2);
                    MeasureCenter.getInstance().measureRT.setType(1);
                    MeasureCenter.getInstance().measureRT.setSlopeangle(slopeangle);
                    SysUtils.getInstance().measure_slopeangle = slopeangle;
                    SysUtils.getInstance().saveMeasureParam();
                    showShortToast(getString(R.string.ykqlsspmcl));
                    beginMesure();
                }
                break;
        }
    }

    private final int DIALOG_SET_TOPBAR = 1;
    private final int DIALOG_SET_TOPBAR1 = 2;
    private final int DIALOG_SET_TOPBAR2 = 3;

    @Override
    public void onDialogItemClick(int requestCode, int position, String item) {
        if (position < 0) {
            position = 0;
        }
        switch (requestCode) {
            case DIALOG_SET_TOPBAR:
                onDoNext(position);
                break;
            case DIALOG_SET_TOPBAR1:
                onDoExport(position);
                break;
            case DIALOG_SET_TOPBAR2:
                onMeasurePop(position);
                break;
            default:
                break;
        }
    }

    int mIndex = -1;
    Measure _measureEdit;

    private void onMeasurePop(int pos) {
        if (pos == 0) {
            _measureEdit = mMeasureList.get(mIndex);
            intent = EditTextInfoActivity.createIntent(context, EditTextInfoActivity.TYPE_NICK
                    , getString(R.string.sscl_name), _measureEdit.getName());
            toActivity(intent, REQUEST_TO_EDIT_TEXT_INFO3, false);
        } else {
            Long measureId = mMeasureList.get(mIndex).getId();
            toActivity(MeasureMapActivity.createIntentById(context, measureId), REQUEST_MEASURE);
        }
    }

    //pos   0-GPX   1-KML  2-SHP
    private void onDoExport(int pos) {
        showProgressDialog(R.string.loading);
        final int style = pos;
        runThread(TAG + "daochuData", new Runnable() {
            @Override
            public void run() {
                doDaochu(style);
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        if (style == 2) {
                            finishSHPDaochu();
                        } else {
                            finishDaochu();
                        }
                    }
                });
            }
        });
    }

    private void finishSHPDaochu() {
        showShortToast("SHP：" + _savePath);
    }

    String _daochuInfo = "";

    private void doDaochu(int style) {
        ArrayList<WayPoint> naviptList = new ArrayList<WayPoint>();
        ArrayList<Route> naviLineList = new ArrayList<Route>();
        ArrayList<WayTrail> naviTrackList = new ArrayList<WayTrail>();

        for (int i = 0; i < mMeasureList.size(); i++) {
            if (hashMap.get(i)) {
                Measure m = mMeasureList.get(i);
                if (m.measurePtList == null) {
                    m.measurePtList = MeasureCenter.getInstance().getMeasurePointListByMeasureId(m.getId());
                }
                WayTrail wt = new WayTrail(m.getName());
                wt.trackPointList = new ArrayList<TrackPoint>();
                for (int j = 0; j < m.measurePtList.size(); j++) {
                    MeasurePoint mp = m.measurePtList.get(j);
                    TrackPoint tp = new TrackPoint();
                    tp.setLL(mp.getLL());
                    tp.setAltitude(mp.getAltitude());
                    tp.setDatetime(System.currentTimeMillis() + j*86400000);
                    wt.trackPointList.add(tp);
                }

                if (m.measurePtList.size() > 0) {
                    //使导出的面积测量封闭
                    MeasurePoint mp = m.measurePtList.get(0);
                    TrackPoint tp = new TrackPoint();
                    tp.setLL(mp.getLL());
                    tp.setAltitude(mp.getAltitude());
                    tp.setDatetime(System.currentTimeMillis() + m.measurePtList.size()*86400000);
                    wt.trackPointList.add(tp);
                }
                naviTrackList.add(wt);
            }
        }

        SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyyMMdd_HHmmss");
        Date curDate = new Date(System.currentTimeMillis());
        String sufix = formatter.format(curDate);
        if (style == 0 || style == 2) {
            IECenter.getInstance().export2GPX(sufix, naviptList, naviLineList, naviTrackList);
            _daochuInfo = String.format(getString(R.string.daochu_format), naviptList.size(), naviLineList.size(),
                    naviTrackList.size(), IECenter.EXDIR_GPX + sufix + ".gpx");

            _savePath = IECenter.EXDIR_GPX + sufix + ".gpx";
        } else {
            IECenter.getInstance().export2KML(sufix, naviptList, naviLineList, naviTrackList);
            _daochuInfo = String.format(getString(R.string.daochu_format), naviptList.size(), naviLineList.size(),
                    naviTrackList.size(), IECenter.EXDIR_KML + sufix) + ".KML";

            _savePath = IECenter.EXDIR_KML + sufix + ".KML";
        }

        if (style == 2) {
            //导出的SHP路径
            String strSHPRoot = Environment.getExternalStorageDirectory() + "/SHP/";
            if (!SysUtils.getInstance().is240x320) {
                strSHPRoot = Start.RUNDIR + "/HCMap_EX/SHP/";
            }
            final String strSHPDir = strSHPRoot + formatter.format(curDate) + "/";
            File file2 = new File(strSHPDir);
            if (!file2.exists()) {
                file2.mkdirs();
            }
            String command = sufix + " -o " + strSHPDir + sufix + " " + _savePath;
            int result = JniUtils.getInstance().receiveCMD(command);
            if (result == 0) {
                _savePath = strSHPDir;
            } else {
                _savePath = "导出异常, ret=" + result;
            }
        }
    }

    String _savePath = "";
    //    /storage/sdcard0/BHCMapper/rundirVersion_1.0.1
    private void finishDaochu() {
        _daochuInfo = _daochuInfo.replace("/mnt/sdcard2", "SD卡");
        new AlertDialog(context, getString(R.string.daochu_cwd), _daochuInfo, false, 186, this).show();

        mEditMode = 0;
        findViewById(R.id.ctrl_measure_edit).setVisibility(View.GONE);
        findViewById(R.id.ctrl_measure_add).setVisibility(View.VISIBLE);

        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }

        if (requestCode == 0) {
            delSelected();
        } else if (requestCode == 1) {
            clearListData();
        } else if (requestCode == 11) {
            doRealtimeareaSave();
        }

    }

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<Integer, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.measurelist_view, parent, false);
            }

            TextView iv_icon = BaseViewHolder.get(convertView, R.id.iv_icon);
            TextView tv_name = BaseViewHolder.get(convertView, R.id.tv_name);
            TextView tv_distance = BaseViewHolder.get(convertView, R.id.tv_distance);
            TextView tv_area = BaseViewHolder.get(convertView, R.id.tv_area);
            TextView iv_check = BaseViewHolder.get(convertView, R.id.iv_check);
            ImageView ivrtimg = BaseViewHolder.get(convertView, R.id.ivrtimg);

            iv_check.setTypeface(AppContext.getInstances().getIconfont());
            iv_icon.setTypeface(AppContext.getInstances().getIconfont());

            final Entry<Integer, String> data = getItem(position);
            int index = data.getKey();
            if (index >= 0 && index < mMeasureList.size()) {

                Measure measure = mMeasureList.get(index);
                String name = measure.getName();
                if (measure.getAreatype() == 1) {
                    name = "[" + getString(R.string.dkmjcl) + "]" + name;
                } else if (measure.getAreatype() == 2) {
                    name = "[" + getString(R.string.pmcl) + "]" + name;
                }
                tv_name.setText(name);
                double perimeter = 0;
                if (measure.getType() != 0) {
                    //测面 周长
                    perimeter = measure.getPerimeter();
                    if (perimeter == 0) {
                        if (measure.measurePtList == null) {
                            measure.measurePtList = MeasureCenter.getInstance().getMeasurePointListByMeasureId(measure.getId());
                        }
                        ArrayList<LonLat> measureposlist = new ArrayList<LonLat>();
                        int size = measure.measurePtList.size();
                        for (int i = 0; i < size; i++) {
                            measureposlist.add(measure.measurePtList.get(i).getLL());
                        }
                        perimeter = Utils.calculatePerimeter(measureposlist);
                        measure.setPerimeter(perimeter);
                    }
                    if (measure.getAreatype() == 1) {
                        perimeter = measure.getLength();
                    }
                } else {
                    //测距
                    perimeter = measure.getLength();
                    if (perimeter == 0) {
                        if (measure.measurePtList == null) {
                            measure.measurePtList = MeasureCenter.getInstance().getMeasurePointListByMeasureId(measure.getId());
                        }
                        ArrayList<LonLat> measureposlist = new ArrayList<LonLat>();
                        int size = measure.measurePtList.size();
                        for (int i = 0; i < size; i++) {
                            measureposlist.add(measure.measurePtList.get(i).getLL());
                        }
                        perimeter = Utils.calculateLen(measureposlist);
                        measure.setLength(perimeter);
                    }
                }

                String DIS[] = SysUtils.getInstance().getDistanceStr((int) perimeter);
                tv_distance.setText(DIS[0] + DIS[1]);

                tv_area.setVisibility(View.GONE);
                if (measure.getType() == 0) {
                    iv_icon.setText(R.string.fonticon_measure_0);
                } else if (measure.getType() == 1) {
                    iv_icon.setText(R.string.fonticon_measure_1);
                    double area = measure.getArea();
                    if (measure.getAreatype() == 1) {
                        area = measure.getAreade();
                    } else if (measure.getAreatype() == 2) {
                        double aa = Math.cos(measure.getSlopeangle());
                        if (aa != 0) {
                            area /= aa;
                        }
                        if (area < 0) {
                            area = -area;
                        }
                    }

                    String ARE[] = SysUtils.getInstance().getAreaStr(area);
                    tv_area.setText(ARE[0] + ARE[1]);
                    tv_area.setVisibility(View.VISIBLE);
                }

                iv_check.setVisibility(View.GONE);
                if (mEditMode > 0) {
                    ivrtimg.setVisibility(View.GONE);
                    boolean isSelected = hashMap.get(index);
                    iv_check.setVisibility(View.VISIBLE);
                    if (isSelected) {
                        iv_check.setText(R.string.fonticon_xz);
                    } else {
                        iv_check.setText(R.string.fonticon_wxz);
                    }
                } else {
                    ivrtimg.setVisibility(View.VISIBLE);
                }
            }

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }

}